//
//  ViewController.swift
//  Swift自定义转成动画练习
//
//  Created by liyang on 16/8/3.
//  Copyright © 2016年 liyang. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    // MARK:- false是消失，true是弹出
    private lazy var isPresent : Bool = false
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // 1、创建要被modal出的控制器Vc
        let modalVc = ModalViewController()
        // 2、设置转场的代理
        modalVc.transitioningDelegate = self
        // 3、设置modal样式
        modalVc.modalPresentationStyle = .Custom
        presentViewController(modalVc, animated: true, completion: nil)
    }
}
// MARK: - 遵循转场代理方法
extension ViewController : UIViewControllerTransitioningDelegate {
    // 目的改变要被modal出来的控制器的frame
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        return LYPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
    
    // 目的：设置弹出动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    // 目的：设置消失动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}
// MARK: - 转场动画代理
extension ViewController : UIViewControllerAnimatedTransitioning {
    // 转场动画的时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    // 转场动画的上下文Context
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresent ? animateTransitionForPresent(transitionContext) : animateTransitionForDismiss(transitionContext)
    }
    
    // 弹出的动画
    func animateTransitionForPresent(transitionContext: UIViewControllerContextTransitioning) {
        // 1、获取弹出的view
        let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        // 2、将弹出的View添加到containerView上
        transitionContext.containerView()?.addSubview(presentView)
        // 3、执行动画
        presentView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        presentView.layer.anchorPoint = CGPointMake(0.5, 0) // 默认这个点在center，我们希望他在上面靠中间的位置
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            // 动画执行完毕，还原形变
            presentView.transform = CGAffineTransformIdentity
        }) { (_) in
            // 告诉上下文，动画执行完毕
            transitionContext.completeTransition(true)
        }
    }
    
    // 消失的动画
    func animateTransitionForDismiss(transitionContext: UIViewControllerContextTransitioning) {
        // 1、获取消失的view
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        // 2、执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            dismissView.transform = CGAffineTransformMakeScale(1.0, 0.001)
        }) { (_) in
            dismissView .removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
}


